package com.starblink.message.messagelikeorcomment.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starblink.basic.apollo.ext.ApolloValSetExt.apolloValue
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.user.UserRelationCodeEnum
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.android.basic.util.post.PostUtils
import com.starblink.rocketreserver.FetchUserMessagesV2Query
import com.starblink.basic.style.R
import com.starblink.basic.util.CommUtils
import com.starblink.message.databinding.ItemMessageFollowerBinding


class MessageFollowerAdapter :
    BaseQuickAdapter<FetchUserMessagesV2Query.FetchUserMessagesV2, MessageFollowerAdapter.VH>() {

    class VH(var binding: ItemMessageFollowerBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        val binding = ItemMessageFollowerBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return VH(binding)
    }

    override fun onBindViewHolder(
        holder: VH,
        position: Int,
        item: FetchUserMessagesV2Query.FetchUserMessagesV2?
    ) {
        item?.apply {
            ImageUtils.loadImage(
                context,
                friend.headImage ?: "",
                holder.binding.ivUserHead,
                ImageUtils.placeholderAvatarImage,
                ImageUtils.placeholderAvatarImage
            )

            holder.binding.tvUserName.text =
                if (friend.deleted == true) context.getString(R.string.account_deleted)
                else friend.userName ?: friend.username ?: context.getString(R.string.unknown)

            holder.binding.tvContent.visibleOrGone(!friend.description.isNullOrEmpty()) {
                text = friend.description
            }

            fun setFollowState(relationCode: Int) {
                when (relationCode) {
                    UserRelationCodeEnum.MYSELF.code -> {
                        // 我自己
                        holder.binding.tvFollow.visibleOrGone(false)
                    }
                    UserRelationCodeEnum.NOTHING.code, UserRelationCodeEnum.FANS.code -> {
                        // 我未关注他
                        holder.binding.tvFollow.visibleOrGone(true)
                        holder.binding.tvFollow.text =
                            CommUtils.getString(com.starblink.basic.style.R.string.add_follow)
                        holder.binding.tvFollow.isSelected = false
                    }
                    UserRelationCodeEnum.FOLLOW.code, UserRelationCodeEnum.BOTH.code -> {
                        // 我已关注他
                        holder.binding.tvFollow.visibleOrGone(true)
                        holder.binding.tvFollow.text =
                            CommUtils.getString(com.starblink.basic.style.R.string.following)
                        holder.binding.tvFollow.isSelected = true
                    }
                }
            }
            setFollowState(friend.relationCode)
            holder.binding.tvFollow.click {
                CommonMutateManage.followUser(
                    context,
                    friend.id,
                    friend.relationCode,
                    {
                        // 刷新状态
                        setFollowState(
                            if (PostUtils.followed(friend.relationCode)) friend.relationCode - 1
                            else friend.relationCode + 1
                        )
                    })
                { relationCode ->
                    friend.apolloValue("relationCode", relationCode)
                    setFollowState(relationCode)
                }
            }
        }
    }
}